package cn.client.ui.view.main.mindfulness

import android.widget.Toast
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import cn.client.R
import cn.client.data.remoteData.bean.BuildGroupApply
import cn.client.data.userStateManager
import cn.client.ui.components.BaseTopBar
import cn.client.ui.components.SubmitButton
import kotlinx.coroutines.launch


// 正念团体创建页面
@Composable
fun MindfulnessGroupBuildScreen(
    navController: NavController,
    viewModel: MindfulnessViewModel = hiltViewModel()
) {

    val userIdState = userStateManager.userId.collectAsState(initial = -1)
    val userId = userIdState.value

    var groupName: String by remember { mutableStateOf("") }
    var groupInfo: String by remember { mutableStateOf("") }
    var numLim: String by remember { mutableStateOf("") }
    val context = LocalContext.current
    val coroutineScope = rememberCoroutineScope()

    LaunchedEffect(Unit) {
        viewModel.pageEvents.collect {
            when (it) {
                is MindfulnessViewModel.PageEvent.Error -> {
                    Toast.makeText(context, "提示：${it.error}", Toast.LENGTH_LONG).show()
                }

                is MindfulnessViewModel.PageEvent.Success -> {
                    // 返回上一级
                    navController.navigateUp()
                }

                else -> {}
            }
        }
    }

    Scaffold(
        topBar = {
            BaseTopBar(title = "创建正念团体",
                onClickArrowBack = {
                    // 返回上一级
                    navController.navigateUp()
                })
        }
    ) { it ->
        Column(
            modifier = androidx.compose.ui.Modifier
                .padding(it)
                .fillMaxSize()
        ) {
            HomeSection(title = "请输入团体名称") {
                TextField(
                    value = groupName,
                    onValueChange = { str -> groupName = str },
                    placeholder = { Text(stringResource(R.string.mindfulness_input_info)) },
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(5.dp)
                )
            }
            HomeSection(title = "请输入团体人数") {
                TextField(
                    value = numLim,
                    onValueChange = { str -> numLim = str },
                    placeholder = { Text(stringResource(R.string._input_number)) },
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(5.dp)
                )
            }
            HomeSection(title = "请输入团体简介") {
                TextField(
                    value = groupInfo,
                    onValueChange = { str -> groupInfo = str },
                    placeholder = { Text(stringResource(R.string.mindfulness_input_info)) },
                    modifier = Modifier
                        .fillMaxWidth()
                        .wrapContentHeight()
                        .padding(5.dp)
                )
            }
            // 提交
            SubmitButton(
                onClickSubmitButton = {
                    coroutineScope.launch {
                        val build = BuildGroupApply(
                            founder = userId,
                            groupName = groupName,
                            groupInfo = groupInfo,
                            numLim = numLim.toInt(),
                        )
                        viewModel.dispatch(
                            MindfulnessViewModel.PageIntent.BuildMindfulnessGroup(
                                build
                            )
                        )
                    }
                }
            )
        }
    }
}
